Skip to content

[CALCITE-7825] RelMdTableReferences does not account for tables in RexSubQuery expressions or Correlate - #5298

Merged
mihaibudiu merged 1 commit into
apache:mainfrom
ehds:hds/fix-table-references-missing-sub-query
Oct 1, 2026
Merged

mihaibudiu merged 1 commit into
apache:mainfrom
ehds:hds/fix-table-references-missing-sub-query

Conversation

@ehds

@ehds ehds commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-7825
This is the follow-up to the #5288. @zzwqqq @vlsi Could you please review these changes?

RelMdTableReferences misses tables in RexSubQuery expressions in Project, Filter, Join, and Calc. It also has no Correlate handler, so rewriting a sub-query can change the result to null.
Other nodes also return null despite having determinable table references: Values should return an empty set, unary nodes should pass through their input references, and RepeatUnion and Combine should merge theirs.

Changes Proposed

Extend RelMdTableReferences to cover sub-queries, Correlate, and other nodes that previously returned null despite having determinable table references. Refactor Join to use the shared references extractor.

@ehds ehds changed the title fix [CALCITE-7825] RelMdTableReferences does not account for tables in RexSubQuery expressions or Correlate Sep 28, 2026
assertNull(tableReferences);
}

@Test void testTableReferencesValues() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are not easy for a human to review, I hope they are right

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests use SQL or RelBuilder to construct the different Rel types being tested, following the same approach as the existing tests.I've checked each plan and its expected table references.

@mihaibudiu
mihaibudiu self-requested a review September 28, 2026 18:20
* Table references from TableFunctionScan.
*
* <p>Returns an empty set if there are no inputs, and {@code null} if the table
* references of any input cannot be determined. Tables accessed internally

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why delete this comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shared behavior is already documented at the class level, and this PR also includes tables referenced by sub-queries in TableFunctionScan’s RexCall. Therefore, I simplified the comment to keep it consistent with the other methods.


/** Table references from Snapshot. */
public @Nullable Set<RelTableRef> getTableReferences(Snapshot rel, RelMetadataQuery mq) {
return mq.getTableReferences(rel.getInput());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to analyze the period operand to?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the period expression should also be inspected. For example, MariaDB allows a sub-query here. I've updated the handler accordingly.


/** Table references from Match. */
public @Nullable Set<RelTableRef> getTableReferences(Match rel, RelMetadataQuery mq) {
return mq.getTableReferences(rel.getInput());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't you need to analyze the pattern and measures operands?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've updated the handler to inspect the pattern, measures, and other RexNode expressions in Match as well.

}

/** Table references from Spool. */
public @Nullable Set<RelTableRef> getTableReferences(Spool rel, RelMetadataQuery mq) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apparently there are still some classes not handled, like Window, Sort, TableModify

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Window, Sort, and TableModify already had handlers in the existing code.

List<RelNode> inputs, RelMetadataQuery mq) {
List<RelNode> inputs, List<? extends RexNode> expressions, RelMetadataQuery mq) {
final List<RelNode> rels = new ArrayList<>(inputs);
final RexVisitorImpl<Void> visitor = new RexVisitorImpl<Void>(true) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can reuse the SubQueryCollector visitor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing SubQueryCollector exposed only collect(Project), and its visitSubQuery stopped after recording a sub-query, so it could miss sub-queries nested in a sub-query’s operands.
It also had no callers in the current codebase. I updated it to accept a deep option and reused the visitor in RelMdTableReferences to scan each expression with deep=true.

@sonarqubecloud

Copy link
Copy Markdown

@mihaibudiu

Copy link
Copy Markdown
Contributor

Please squash the commits for merging.

@mihaibudiu mihaibudiu added the LGTM-will-merge-soon Overall PR looks OK. Only minor things left. label Oct 1, 2026
@ehds
ehds force-pushed the hds/fix-table-references-missing-sub-query branch from 08ff544 to 7392942 Compare October 1, 2026 03:33
@ehds

ehds commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Please squash the commits for merging.

Done.

@mihaibudiu
mihaibudiu merged commit 2d28ce3 into apache:main Oct 1, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

LGTM-will-merge-soon Overall PR looks OK. Only minor things left.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants